package com.crazyhat.crazyhat.utils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TokenBlacklistUtil {
    private static Map<String, LocalDateTime> tokenBlacklist = new HashMap<>();

    public static void addToBlacklist(String token, LocalDateTime expirationTime) {
        tokenBlacklist.put(token, expirationTime);
    }

    public static boolean isTokenBlacklisted(String token) {
        cleanExpiredTokens();
        return tokenBlacklist.containsKey(token);
    }

    public static void removeFromBlacklist(String token) {
        tokenBlacklist.remove(token);
    }

    private static void cleanExpiredTokens() {
        LocalDateTime now = LocalDateTime.now();
        Iterator<Map.Entry<String, LocalDateTime>> iterator = tokenBlacklist.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, LocalDateTime> entry = iterator.next();
            if (entry.getValue().isBefore(now)) {
                iterator.remove();
            }
        }
    }
}